package gui;

import gui.tool.GridBagPanel;

import javax.swing.*;
import java.awt.*;

import static gui.MainFrame.sql;

public class LoginPage extends JPanel {
    public LoginPage () {
        setLayout(new BorderLayout());
        JTextField t = new JTextField(20);
        JPasswordField p = new JPasswordField(20);
        JButton log_in = new JButton("登入");
        JButton sign_in = new JButton("注册");
        add(new GridBagPanel().add(new JLabel("用户名："), 0, 0, 1, 1).add(new JLabel("密码："), 0, 1, 1, 1)
                .add(t, 1, 0, 2, 1).add(p, 1, 1, 2, 1), BorderLayout.CENTER);
        log_in.addActionListener(e -> {
            if (sql.password.log_in(t.getText(), new String(p.getPassword()))) {
                MainFrame.setCenter(1080, 720);
                MainFrame.update(new MainPage());
            }
        });
        sign_in.addActionListener(e -> {
            if (sql.password.sign_in(t.getText(), new String(p.getPassword()))) {
                JOptionPane.showMessageDialog(null,
                        "<html><body><p>注册成功.</p><table border=\"1\"><tr><td>用户名</td><td>密码</td></tr><tr><td>"
                                + t.getText() + "</td><td>" + new String(p.getPassword())
                                + "</td></tr></table></body></html>",
                        "提示", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "注册失败.", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
        add(new GridBagPanel().add(log_in, 0, 0, 1, 1, 0, 5).add(sign_in, 1, 0, 1, 1, 0, 5), BorderLayout.SOUTH);
    }
}